package com.shop.dao;

import com.shop.bean.PurchaseIntent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class PurchaseDAO extends BaseDAO {

    // 提交购买意向
    public void addPurchaseIntent(PurchaseIntent intent) {
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(
                     "INSERT INTO purchase_intent(product_id,buyer_name,location,trade_time,phone,amount,deposit,status) " +
                     "VALUES(?,?,?,?,?,?,?,1)")) {
            ps.setInt(1, intent.getProductId());
            ps.setString(2, intent.getBuyerName());
            ps.setString(3, intent.getLocation());
            ps.setString(4, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(intent.getTradeTime()));
            ps.setString(5, intent.getPhone());
            ps.setDouble(6, intent.getAmount());
            ps.setDouble(7, intent.getDeposit());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 获取商品的所有购买意向
    public List<PurchaseIntent> getIntentByProductId(int productId) {
        List<PurchaseIntent> list = new ArrayList<>();
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(
                     "SELECT * FROM purchase_intent WHERE product_id=? ORDER BY create_time DESC")) {
            ps.setInt(1, productId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                PurchaseIntent intent = new PurchaseIntent();
                intent.setId(rs.getInt("id"));
                intent.setProductId(rs.getInt("product_id"));
                intent.setBuyerName(rs.getString("buyer_name"));
                intent.setLocation(rs.getString("location"));
                intent.setTradeTime(rs.getTimestamp("trade_time"));
                intent.setPhone(rs.getString("phone"));
                intent.setAmount(rs.getDouble("amount"));
                intent.setDeposit(rs.getDouble("deposit"));
                intent.setStatus(rs.getInt("status"));
                intent.setRejectReason(rs.getString("reject_reason"));
                intent.setCreateTime(rs.getTimestamp("create_time"));
                list.add(intent);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    // 更新购买意向状态
    public void updateIntentStatus(int intentId, int status, String rejectReason) {
        try (Connection conn = getConnection();
             PreparedStatement ps = conn.prepareStatement(
                     "UPDATE purchase_intent SET status=?, reject_reason=? WHERE id=?")) {
            ps.setInt(1, status);
            ps.setString(2, rejectReason);
            ps.setInt(3, intentId);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}